package me.samlss.lighter;

import me.samlss.lighter.interfaces.LighterInternalAction;
import me.samlss.lighter.interfaces.OnLighterListener;
import me.samlss.lighter.interfaces.OnLighterViewClickListener;
import me.samlss.lighter.parameter.LighterParameter;
import me.samlss.lighter.util.Preconditions;
import me.samlss.lighter.view.LighterView;
import ohos.aafwk.ability.Ability;
import ohos.agp.components.ComponentContainer;

/**
 * @author: zhu_zhonglin
 * @time: 2021/4/25
 */
public class Lighter implements LighterInternalAction {
    private LighterInternalImpl mInternalImpl;

    private Lighter(ComponentContainer rootView){
        mInternalImpl = new LighterInternalImpl(rootView);
    }

    public Lighter(ComponentContainer rootView, boolean canDrag) {
        mInternalImpl = new LighterInternalImpl(rootView, canDrag);
    }

    private Lighter(Ability ability){
        mInternalImpl = new LighterInternalImpl(ability);
    }

    /**
     * 获取Lighter内部实现类
     * @return LighterInternalImpl
     */
    public LighterInternalImpl getInternalImpl() {
        return mInternalImpl;
    }

    /**
     * 实例化Lighter
     * @param ability ability
     * @return Lighter对象
     */
    public static Lighter with(Ability ability){
        Preconditions.checkNotNull(ability,
                "You can not show a highlight view on a null ability.");
        return new Lighter(ability);
    }

    /**
     * Create a {@link Lighter} with an {@link Ability} br></>
     * The highlighted view will fill the entire root view.
     *
     * @param rootView The root view that will be attach by the {@link LighterView}.
     *
     * @return Lighter
     */
    public static Lighter with(ComponentContainer rootView){
        Preconditions.checkNotNull(rootView,
                "You can not show a highlight view on a null root view.");
        return new Lighter(rootView);
    }

    /**
     * Create a {@link Lighter} with an {@link Ability} br></>
     * The highlighted view will fill the entire root view.
     *
     * @param rootView The root view that will be attach by the {@link LighterView}.
     * @param canDrag The lighter view that will drag {@link LighterView}.
     * @return Lighter
     */
    public static Lighter with(ComponentContainer rootView, boolean canDrag){
        Preconditions.checkNotNull(rootView,
                "You can not show a highlight view on a null root view.");
        return new Lighter(rootView, canDrag);
    }

    /**
     * Add highlight parameter <br></>
     *
     * Can highlight multiple parameters simultaneously.
     *
     * @param lighterParameters The specified highlight parameter
     * @return Lighter
     */
    public Lighter addHighlight(LighterParameter...lighterParameters){
        mInternalImpl.addHighlight(lighterParameters);
        return this;
    }

    /**
     * Set background color of the highlighted view.
     *
     * @param color The background color, default is .
     * @param alpha The background alpha, default is 1 0.0f ~ 1.0f
     * @return Lighter
     */
    public Lighter setBackgroundColor(int color,float alpha){
        mInternalImpl.setBackgroundColor(color, alpha);
        return this;
    }

    /**
     * Set whether to automatically show the next highlighted view, the default is true.
     * If true, when click the {@link LighterView } & tip view, will show the next highlighted view automatically.
     * Otherwise you need to invoke {@link #next()} manually.
     *
     * @param autoNext autoNext
     * @return Lighter
     */
    public Lighter setAutoNext(boolean autoNext){
        mInternalImpl.setAutoNext(autoNext);
        return this;
    }

    /**
     * Set whether to intercept the click event of the {@link LighterView}. If it's true, it will not process the click event.
     * You need to manually handle the next event, and the view under the {@link LighterView} can be clicked.
     * And the {@link OnLighterViewClickListener} will not be invoked.
     *
     * @param intercept
     * @return Lighter
     */
    public Lighter setIntercept(boolean intercept){
        mInternalImpl.setIntercept(intercept);
        return this;
    }

    /**
     * Set click listener of {@link LighterView} & tip view.
     * @param clickListener
     *
     * @return Lighter
     */
    public Lighter setOnClickListener(OnLighterViewClickListener clickListener){
        mInternalImpl.setOnClickListener(clickListener);
        return this;
    }

    /**
     * Set click listener of {@link LighterView} & tip view.
     *
     * @param onLighterListener onLighterListener
     * @return Lighter
     */
    public Lighter setOnLighterListener(OnLighterListener onLighterListener){
        mInternalImpl.setOnLighterListener(onLighterListener);
        return this;
    }

    @Override
    public boolean hasNext() {
        return mInternalImpl.hasNext();
    }

    @Override
    public void next() {
        mInternalImpl.next();
    }

    /**
     * Start to show highlight
     */
    public void show(){
        mInternalImpl.show();
    }

    @Override
    public boolean isShowing() {
        return mInternalImpl.isShowing();
    }

    @Override
    public void dismiss() {
        mInternalImpl.dismiss();
    }
}
